package com.qk.service.impl;

import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService{

    @Autowired
    ClueMapper clueMapper;

    @Autowired
    BusinessMapper businessMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    //获取首页概览数据
    @Override
    public OverviewVO getOverOiew() {
        //1.查询redis缓存中的数据
        log.info("从redis中,查询线索和商机的概览数据");
        Object object = redisTemplate.opsForValue().get("overview");
        if (object != null){
            log.info("从redis中,查询线索和商机的概览数据");
            return (OverviewVO) object;
        }

        //2.查询数据库
        //2.1获取线索的所有数据
        OverviewVO ClueviewVO =clueMapper.getClueOverviewVO();
        //2.2获取商机的所有数据
        OverviewVO BusinessviewVO =businessMapper.getBusinessOverviewVO();
        //2.3封装结果并返回
        BeanUtils.copyProperties(ClueviewVO, BusinessviewVO,"businessTotal","businessWaitAllot", "businessWaitFollow", "businessFollowing", "businessFalse", "businessConvertCustomer");
        //2.4将数据缓存到redis中
        log.info("将数据缓存到redis中");
        redisTemplate.opsForValue().set("overview", BusinessviewVO,5, TimeUnit.MINUTES);
        //3.返回结果
        return BusinessviewVO;
    }
}
